/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.admin.client.components;

import java.util.ArrayList;
import java.util.List;

import com.elf.gwt.admin.client.AdminComponentController;
import com.elf.gwt.common.client.model.AbstractModel;
import com.elf.gwt.common.client.model.LanguageModel;
import com.elf.gwt.common.client.utils.CommonUtils;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Hidden;
import com.google.gwt.user.client.ui.TextBox;

/**
 *  
 */
public class LanguagesAdministrationComponent extends EditTableComponent
{	
	protected LanguageModel emptyLanguageModel;
	protected LanguageModel languageModel;
	protected List<LanguageModel> languageModels;

	protected String defaultCode = "";
	protected String currentCode = "";

	/**
	 * Constructor
	 */
	public LanguagesAdministrationComponent(AdminComponentController controller, String stylePrefix)
	{
		super(controller, stylePrefix);

		emptyLanguageModel = new LanguageModel(-1, "", "", "", false);
		languageModel = new LanguageModel(1, "English", "en", "", true);
		languageModels = new ArrayList<LanguageModel>();
	}


	protected void buildMetaModel(JSONObject jsonObject) throws Exception
	{
		defaultCode = AbstractModel.convertToString(jsonObject, "default_code");
		currentCode = AbstractModel.convertToString(jsonObject, "current_code");
	}

	protected void buildModel(JSONArray jsonArray) throws Exception
	{
		languageModels.clear();
		super.buildModel(jsonArray);
	}

	protected void buildModel(JSONObject jsonObject) throws Exception
	{
		LanguageModel model = new LanguageModel(jsonObject, defaultCode);
		languageModels.add(model);
		if (model.getCode().equals(currentCode))
		{
			languageModel.update(model);
		}
	}

	public LanguageModel getLanguageModel(int index) {
		return languageModels.get(index);
	}

	public LanguageModel getLanguageModel() {
		return languageModel;
	}

	public String getImageDirectory(LanguageModel model)
	{
		return CommonUtils.getImageDirectory() + getHandle() + "/" + model.getCode() + "/";
	}

	public void buildFromModel() throws Exception
	{
		numberOfTableEntries = languageModels.size();
		if (edit)
			numberOfTableColumns = 5;
		else
			numberOfTableColumns = 4;
		extraRows = 2;
		super.buildFromModel();
	}

	public void displayHeader(Grid grid) {
		//	languages_id	name	code	image	directory	sort_order
		grid.setText(0, 0, "Name");
		grid.setText(0, 1, "Code");
		grid.setText(0, 2, "Image");
		grid.setText(0, 3, "Sort Order");
		if (edit) grid.setText(0, 4, "Mark for Deletion");
	}

	public void displayRow(Grid grid, int row, int entryIndex) {
		displayRow(grid, row, languageModels.get(entryIndex));
	}

	public void displayEmptyRow(Grid grid, int row) {
		displayRow(grid, row, emptyLanguageModel);
	}

	public void displayRow(Grid grid, int row, LanguageModel model) {
		if (edit) {

			Hidden idHidden = new Hidden();
			idHidden.setName("languageId");
			idHidden.setValue(String.valueOf(model.getId()));
			elements.put("languageId-" + row, idHidden);

			TextBox nameTextBox = new TextBox();
			nameTextBox.setWidth("80px");
			nameTextBox.setText(model.getName());
			elements.put("languageName-" + row, nameTextBox);
			grid.setWidget(row, 0, nameTextBox);

			TextBox codeTextBox = new TextBox();
			codeTextBox.setWidth("80px");
			codeTextBox.setText(model.getCode());
			elements.put("languageCode-" + row, codeTextBox);
			grid.setWidget(row, 1, codeTextBox);

			TextBox imageTextBox = new TextBox();
			imageTextBox.setWidth("80px");
			imageTextBox.setText(model.getImage());
			elements.put("languageImage-" + row, imageTextBox);
			grid.setWidget(row, 2, imageTextBox);

			TextBox sortOrderTextBox = new TextBox();
			sortOrderTextBox.setWidth("80px");
			sortOrderTextBox.setText(String.valueOf(row));
			elements.put("languageSortOrder-" + row, sortOrderTextBox);
			grid.setWidget(row, 3, sortOrderTextBox);

			if (model.getId() > 0) {
				CheckBox deleteCheckBox = new CheckBox();
				elements.put("languageDelete-" + row, deleteCheckBox);
				grid.setWidget(row, 4, deleteCheckBox);
			}
		}
		else {
			grid.setText(row, 0, model.getName());
			grid.setText(row, 1, model.getCode());
			grid.setText(row, 2, model.getImage());
			grid.setText(row, 3, String.valueOf(row));
		}
	}
}
